<template>
  <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" size="small">
    <el-form-item label="配置项" prop="configurationKey">
      <el-select v-model="ruleForm.configurationKey" placeholder="请选择配置项" class="block">
        <el-option v-for="(it, i) in $attrs.types" :key="i" :value="it.value" :label="it.label"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="配置值" prop="configurationValue">
      <el-input v-model.trim="ruleForm.configurationValue" placeholder="请输入配置值"></el-input>
    </el-form-item>
<!--
   <template v-if="isUpdateAction">
      <el-form-item label="校验正则表达式" prop="valuePattern">
      <el-input v-model.trim="ruleForm.valuePattern" readonly></el-input>
    </el-form-item>

     <el-form-item label="校验失败提示语" prop="valuePatternErrorMessage">
      <el-input v-model.trim="ruleForm.valuePatternErrorMessage" readonly></el-input>
    </el-form-item>

     <el-form-item label="配置项简介" prop="valueSummary">
      <el-input v-model.trim="ruleForm.valueSummary" type="textarea" :rows="4" readonly></el-input>
    </el-form-item>
   </template> -->
  </el-form>
</template>

<script>
/* Mixins
------------------------------- */
import { Form } from '@/mixins'

/* Api
------------------------------- */
import { checkSystemConfigKey } from '@/api'

/* Model
------------------------------- */

import { configModel, primaryKey } from '../model'
export default {
  mixins: [Form('ruleForm', configModel)],
  data () {
    const asyncCheckSystemConfigKey = (rule, value, callback) => {
      return this.checkSystemConfigKey(
        {
          key: value,
          id: this.ruleForm[primaryKey]
        },
        {
          onOk ({ data }) {
            !data ? callback(new Error('配置项编码已存在')) : callback()
          },
          onError ({ message }) {
            callback(new Error(message))
          }
        }
      )
    }
    return {
      ruleForm: Object.assign({}, configModel),
      rules: {
        configurationName: [
          { required: true, message: this.$t('required', { field: '配置项' }) }
        ],
        configurationKey: [
          {
            required: true,
            message: this.$t('required', { field: '配置项编码' })
          },
          { validator: asyncCheckSystemConfigKey, trigger: 'blur' }
        ],
        configurationValue: [
          {
            required: true,
            message: this.$t('required', { field: '配置项值' })
          }
        ]
      }
    }
  },
  methods: {
    checkSystemConfigKey ({ key, id }, { onOk, onError }) {
      return this.Catch(checkSystemConfigKey, {
        onOk,
        onError
      })(null, key, id)
    }
    // --------------------------------------  request --------------------------------------
    // -------------------------------------- !request --------------------------------------
  },
  computed: {
    isUpdateAction () {
      return !!this.ruleForm[primaryKey]
    }
  },
  created () {
    this.$nextTick(() => {
      if (this.isUpdateAction) {
        let { valuePatternErrorMessage, valuePattern } = this.record
        if (valuePattern) {
          try {
            let pattern = new RegExp(valuePattern.replace(/\\+/, '\\'))
            this.rules.configurationValue.push({
              pattern,
              message: valuePatternErrorMessage
            })
          } catch (e) {

          }
        }
      }
    })
  },
  mounted () {
    this.watchSelect('configurationKey', 'configurationName', this.$attrs.types, (val, oldVal) => {
      let cfg = this.$attrs.types.find(it => it.value === val)
      if (cfg && cfg.ori.valuePattern) {
        let pattern = new RegExp(cfg.ori.valuePattern.replace(/\\+/, '\\'))
        let prevPatternIndex = this.rules.configurationValue.findIndex(it => Object.prototype.hasOwnProperty.call(it, 'pattern'))
        let newPattern = {
          pattern,
          message: cfg.ori.valuePatternErrorMessage
        }
        if (prevPatternIndex > -1) {
          this.rules.configurationValue[prevPatternIndex] = newPattern
        } else {
          this.rules.configurationValue.push(newPattern)
        }
      }
    })
  }
}
</script>
